package com.kuang.demo.controller;

import com.kuang.demo.bean.BaseRespVo;
import com.kuang.demo.bo.zzy.AdminBo.*;
import com.kuang.demo.bo.zzy.TotalItem;
import com.kuang.demo.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin/admin")
public class AdminController {

    @Autowired
    AdminService adminService;


    //查询全部数据

    @RequestMapping("/list")
    public BaseRespVo zzy1( ListPageLimit listPageLimit){
        TotalItem totalItem = adminService.selectAllList(listPageLimit);

        return BaseRespVo.ok(totalItem);
    }

    //删除数据

    @RequestMapping("/delete")
    public BaseRespVo zzy2(@RequestBody IdNameAvaRole idNameAvaRole){
        adminService.deleteById(idNameAvaRole.getId());

        return BaseRespVo.ok();
    }

    //编辑数据

    @RequestMapping("/update")
    public BaseRespVo zzy3(@RequestBody IdNameAvaRole2 idNameAvaRole2){
        ReturnUpdate returnUpdate = adminService.updateAdmin(idNameAvaRole2);

        return BaseRespVo.ok(returnUpdate);
    }


    //创建数据，判断是否包含avatar
    @RequestMapping("/create")
    public BaseRespVo zzy4(@RequestBody ReturnUpdate returnUpdate){

        //首先判断是否重名
        int i = adminService.selectByUsername(returnUpdate.getUsername());
        if(i != 0){
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrmsg("该管理员已经存在");
            baseRespVo.setErrno(602);
            return baseRespVo;
        }


        ReCreateAdmin reCreateAdmin = adminService.CreateAdmin(returnUpdate);

        return BaseRespVo.ok(reCreateAdmin);
    }

}
